import { ITabViewPanelStore } from '@/core/interface/widgets/store';
import { CtrlController } from './ctrl-controller';
import {
	TabViewPanelActionType,
	ITabViewPanelAbility,
	ITabViewPanelController,
	ITabViewPanelControllerParams,
	IParam,
	ITabViewPanelModel,
	IViewAbility,
} from '../../interface';
import { computedNavData } from '@/core';

/**
 * 分页视图面板部件控制器
 *
 * @export
 * @class TabViewPanelController
 * @extends {CtrlController<ITabViewPanelStore>}
 * @implements {ITabViewPanelController}
 * @template T
 */
export class TabViewPanelController
	extends CtrlController<
		TabViewPanelActionType,
		ITabViewPanelStore,
		ITabViewPanelAbility
	>
	implements ITabViewPanelController<ITabViewPanelStore, ITabViewPanelAbility>
{
	/**
	 * Creates an instance of TabViewPanelController.
	 * @param {ITabViewPanelControllerParams<TabViewPanelActionType, ITabViewPanelAbility>} params
	 * @memberof TabViewPanelController
	 */
	public constructor(
		params: ITabViewPanelControllerParams<
			TabViewPanelActionType,
			ITabViewPanelAbility
		>
	) {
		super(params);
		this.ctrlInit(params);
	}

	/**
	 * 处理部件初始化
	 *
	 * @protected
	 * @param {ITabViewPanelControllerParams<TabViewPanelActionType, ITabViewPanelAbility>} params
	 * @memberof TabViewPanelController
	 */
	protected processCtrlInit(
		params: ITabViewPanelControllerParams<
			TabViewPanelActionType,
			ITabViewPanelAbility
		>
	) {
		super.processCtrlInit(params);
	}

	/**
	 * 获取嵌入视图能力
	 *
	 * @return {*}  {(IPickupGridViewAbility | IPickupTreeViewAbility | undefined)}
	 * @memberof PickupViewPanelController
	 */
	public getEmbedViewAbility(): IViewAbility | undefined {
		if (this.model.embedViewCodeName) {
			return this.getSubAbility<IViewAbility>(this.model.embedViewCodeName);
		}
		return undefined;
	}

	/**
	 * 加载嵌入视图
	 *
	 * @memberof PickupViewPanelController
	 */
	public load(args?: IParam): void {
		const ability = this.getEmbedViewAbility();
		if (ability) {
			ability.load(args);
		}
	}

	/**
	 * @description 获取能力
	 * @return {*}  {ITabViewPanelAbility}
	 * @memberof TabViewPanelController
	 */
	public getAbility(): ITabViewPanelAbility {
		return {
			...super.getAbility(),
			load: this.load.bind(this),
		};
	}

	/**
	 * @description 设置导航参数
	 * @param {IParam} context
	 * @param {IParam} viewParams
	 * @memberof TabViewPanelController
	 */
	public setNavParams(context: IParam, viewParams: IParam): void {
		const model = this.getModel<ITabViewPanelModel>();
		if (
			model.navigateContext &&
			Object.keys(model.navigateContext).length > 0
		) {
			const localContext = computedNavData(
				{},
				context,
				viewParams,
				model.navigateContext
			);
			Object.assign(context, localContext);
		}
		if (model.navigateParams && Object.keys(model.navigateParams).length > 0) {
			const localViewParam = computedNavData(
				{},
				context,
				viewParams,
				model.navigateParams
			);
			Object.assign(viewParams, localViewParam);
		}
		super.setNavParams(context, viewParams);
	}
}
